package com.hospital.wx.controller;

import com.hospital.wx.common.CommonResult;
import com.hospital.wx.service.DoctorAppointmentsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;

@RestController
@RequestMapping("/doctor/appointments")
@Slf4j
@Tag(name = "DoctorAppointmentsController", description = "预约记录接口")
public class DoctorAppointmentsController {

    @Autowired
    private DoctorAppointmentsService doctorAppointmentsService;

    @GetMapping("/selectAll")
    @Operation(summary = "查询所有预约")
    @CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
    public CommonResult selectAll() {
        try {
            ArrayList<HashMap> list = doctorAppointmentsService.selectAll();
            return CommonResult.ok().put("result", list);
        } catch (Exception e) {
            log.error("查询过程中出现错误", e);
            return CommonResult.failed("查询过程中出现错误");
        }
    }

    @PostMapping("/cancel")
    @Operation(summary = "取消预约")
    public CommonResult cancelAppointment(@RequestParam("id") Integer id) {
        try {
            int rows = doctorAppointmentsService.cancelAppointment(id);
            if (rows != 1) {
                return CommonResult.failed("取消预约失败");
            }
            return CommonResult.ok().put("rows", rows);
        } catch (Exception e) {
            log.error("取消预约过程中出现异常", e);
            return CommonResult.failed("取消预约过程中出现异常");
        }
    }
}
